<?php

/** 
* Class used to organize tools of the WebService
* @author Edio Hanoway Kepke <edio@aquiris.com>
* @version 1.0 
* @access public
*/ 

class HelpPost 
{        
    /**
    * Return content array containing error object and data object
    * @access public
    * @param $error Error number
    * @param $data Data
    * @return Array
    */
    public static function getArrayReturn($error, $data = null)
    {        
        return array('content' => array
                                        (
                                            'error' => Error::getError($error),
                                            'data' => $data
                                        )            
                    );    
    }
               
    /**
    * Return data of other Web Service using curl
    * @access public
    * @param $url String Url of the service    
    * @param $usePost boolean method post is used
    * @param $data Dictionary Variables to be send
    * @return Object
    */
    public static function getCurl($url, $data = null, $usePost = false)
    {
                                                
        $curl = curl_init();
        
        curl_setopt($curl, CURLOPT_URL, $url);
        
        curl_setopt ($curl, CURLOPT_POST, $usePost);
                
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            
        //segue redirecionamento
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
                                                                       
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        
        curl_setopt($curl, CURLOPT_HEADER      ,0);
                                                                                                                                                            
        if(is_null($data))
        {
            $output = curl_exec($curl);            
        }
        else
        {
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data, null, '&'));
            
            $output = curl_exec($curl);
        }
                
        $codeerror = curl_getinfo($curl, CURLINFO_HTTP_CODE);
                
        
        curl_close($curl);  
                                
        return self::getArrayReturn($codeerror, $output);
    }
    
    
    /**
    * Get a form to teste service
    * @param $url String Url of the service    
    * @param $data Dictionary Variables to be send 
    * @param $method String method used POST or GET    
    */
    public static function getFormTest($url, $data, $method = 'POST')
    {                        
        $form = '<form action="'.$url.'" method="'.$method.'" enctype="multipart/form-data">';
        
        foreach ($data as $key => $value) 
        {                            
            $form = $form.'<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
        }                        
                
        $form = $form.'<input type="submit" value="'.$url.'" /></form>';
                
        return $form;
    }             
                           
    /**
    * returns value passed via post
    * @access public
    * @param $var name of the variable        
    * @return Object or null
    */
    public static function setVarPost($var)
    {        
        (isset ($_POST[$var]))? ($newvar = $_POST[$var]) : ($newvar = null);
        return $newvar;
    }
                        
    /**
    * validates the variables received post
    * @access public
    * @param $post array containing the variables
    * @return Object or null
    */
    public static function validPost($posts)
    {        
        $count = 0;
        $notValid = array();
        $valid = array();
        $hash_request = $posts['hash'];        
        $hash = "";        
        unset ($posts['hash']);
        $size = count($posts);
        
        
        foreach ($posts as $key => $post) 
        {      
            $hash = $hash.$post;
            if(!(is_null($post)))
            {
               $count++; 
               $valid[]= $key;             
            }
            else
            {
                $notValid[] = $key;
            }
        }            

        $hash = md5($hash.Fum::getKey());

        if($size == $count) 
        {                                                
            if($hash == $hash_request)
            {                                                            
                $isValid = true;
                $erro = HelpPost::getArrayReturn(Error::NOT_EXIST_ERROR);
            }            
            else
            {
               $isValid = false;
               $erro = HelpPost::getArrayReturn(Error::INVALID_HASH);
            }                                
        }            
        else
        {
            $isValid = false;
            $erro = HelpPost::getArrayReturn(Error::INSUFFICIENT_PARAMETERS);
        }                                                            

        return array(
                        'isValid' => $isValid,
                        'notValid'=> $notValid,
                        'valid' => $valid,
                        'error' => $erro
                    );                                     
    } 
    
    /**
    * Send one mail
    * @access public
    * @param $frommaill The email of the remitter
    * @param $fromname The name of the remitter
    * @param $tomail The email of the addressee
    * @param $subject
    * @param $message
    * @return void
    */
    static function sendMail($frommail,$fromname,$tomail,$subject, $message)
    {                
        $config['mailtype'] = 'html';
        $ci =& get_instance();         
        $ci->load->library('email',$config);        
        $ci->email->from($frommail,$fromname);        
        $ci->email->to($tomail);
        $ci->email->subject($subject);
        $ci->email->message($message);	
        $ci->email->send();  
    }          
}

?>
